package atm;

import java.text.SimpleDateFormat;
import java.util.Date;

import domain.Bcard;

/**
 * 
     * @ClassName: Payment
     * @Description: 代缴费充值
     * @author Huang_shengjun
     * @date 2020年5月30日 下午12:37:35
     *
 */
public class Payment extends Transaction{

	public static final int TRANS_UNSTART = 1; // 未开始
	public static final int TRANS_GETDATA = 2; // 获取交易请求
	public static final int TRANS_SUCCESS = 3; // 交易成功
	public static final int TRANS_FAILURE = 4; // 交易失败
	public static final int TRANS_EXIT = 4; // 退出
	public int PAYMENT = 2; // 退出
	Bcard cardDao = Bcard.bcard();
	
	public Payment(Session session, Bcard bcard) {
		super(session, bcard);
		// 当选择取款交易时,需要改变显示屏的显示,需要改变数字键盘的状态
		ATM machine = ATM.getInstance();
		machine.getDisplay().setText("你的余额是"+cardDao.getbBalance()+"，请选择代缴费类型"+"<br>"+"水费:0   话费:1");
		machine.getDigitButton().stateChange(0, 0, "PaymentChooseServlet");
			
	}
	
	/**
	 * 0 水费； 1  话费
	 */
	public void paymentChoose(int flag) {
		// 显示屏更新 数字键盘状态更新 
		this.setState(TRANS_EXIT);
		this.getSession().setState(Session.CHOOSING);
		ATM machine = ATM.getInstance();
		if (flag == 0) {  // 水费
			machine.getDisplay().setText("请输入充值水费金额：");
			machine.getDigitButton().stateChange(1, 0, "PaymentWaterServlet");
		}else if(flag == 1) { // 话费
			machine.getDisplay().setText("请输入充值话费金额：");
			machine.getDigitButton().stateChange(1, 0, "PaymentPhoneServlet");
		}
		else {
			machine.getDisplay().setText("请选择正确的选项"+"<br>"+"水费:0   话费:1");
			machine.getDigitButton().stateChange(0, 0, "PaymentChooseServlet");
		}
	}
	
	
	// 充值
	public void payment(int i) {
		PAYMENT = i;
		ATM machine = ATM.getInstance();
		if (this.getAmount() <= cardDao.getbBalance() && this.getAmount() > 0) {
			try {
				cardDao.editPayment(i,this.getAmount());
				// 显示屏更新 数字键盘状态更新
				this.setState(TRANS_SUCCESS);
				machine.getDisplay().setText("充值成功。你的余额是" + cardDao.getbBalance() + "<br>" + "打印:0 不打印:1");
				machine.getDigitButton().stateChange(0, 0, "PaymentprintServlet");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				this.setState(TRANS_FAILURE);// 4 交易失败
				machine.getDisplay().setText("操作失败，请输入大于0金额，重新操作！" + "<br>" + "确定:1");
				machine.getDigitButton().stateChange(0, 0, "PaymentprintServlet");
			} 
		} else {
			this.setState(TRANS_FAILURE);// 4 交易失败
			machine.getDisplay().setText("请输入正确的金额，重新操作！" + "<br>" + "确定:1");
			machine.getDigitButton().stateChange(0, 0, "PaymentprintServlet");
		}
	}

	
	
	//代缴打印
	public void paymentPrint(int flag) {
		String a = ""; 
		if (PAYMENT == 0) {
			a = "水费充值";
		}else if(PAYMENT == 1) {
			a = "话费充值";
		}
		// 显示屏更新 数字键盘状态更新
		this.setState(TRANS_EXIT);
		this.getSession().setState(Session.CHOOSING);
		ATM machine = ATM.getInstance();
		if (flag == 0) {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
			// 利用html换行符 &#10; 或 &#13;
			machine.getArea().setText("************收据************&#10;&#10;&#10;"
					
					+ "**********充值类型**********&#13;" + "类型：" + a + "&#10;"
				
					+ "**********充值金额**********&#13;" + "金额：" + this.getAmount() + "&#10;"

					+ "************余额************&#13;" + "余额：" + cardDao.getbBalance() + "&#10;"

					+ "************时间************&#13;" + df.format(new Date()) + "&#10;&#10;&#10;" // Date获取当前时间

					+ "************收据************&#10;");
		}
		machine.getDisplay().setText("请选择业务 1:取款 2:存款 3:查询余额 4:转账 5:代缴费 6:修改密码   0:退出 ");
		machine.getDigitButton().stateChange(0, 0, "TransactionServlet");
	}


}
